<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class tool extends CI_Controller {
var $cookieFileName;
	function __construct() {
		
        parent::__construct();
		//$this->load->model("getsite/tvzing");
		$this->load->library('get_data_html');
		$this->cookieFileName = dirname (realpath(__FILE__)).'/'.$this->cookieFileName; 
    }
    public function field($table_name = '') {
        $fields = $this->db->field_data($table_name);
        echo '$fieldData = array();';
        echo "<br>";
        foreach ($fields as $field) {
            if ($field->default == '')
                $data = $this->fieldDefault($field->name, $field->type);
            else
                $data = $field->default;
            echo '$fieldData[\'' . $field->name . '\'] = ' . $data . ';';
            echo "<br>";
        }
        echo 'return $fieldData;';
    }

    function fieldDefault($name, $type = '') {
        $default = "";

        //time
        if (strpos($name, "TIME_"))
            $default = 'time()';
        else if (strpos($name, "_STATUS"))
            $default = '1';
        else if ($type == 'bigint' || $type == 'int')
            $default = '0';
        else
            $default = "''";
        return $default;
    }

    function addProjectPermisionDefault() {
        $arr_code = array('PROJECT', 'TASK', 'TODO');
    }

    function addUserPermisionDefault($userID) {
        $arr_code = array('PROJECT', 'TASK', 'TODO');
        foreach ($arr_code as $code) {
            $array_insert = array();
            $array_insert['RES_CODE'] = $code;
            $array_insert['RES_NAME'] = $code;

            $array_insert['USE_ID'] = $userID;
        }
    }
	
	function check_ref(){
		$this->load->library('user_agent');
		echo '<pre>';
				var_dump($_SERVER);
				echo '</pre>';
	}
	
	function countsoft(){
		$url = 'http://www.download.com.vn/windows';
		$html_content = $this->get_data_html->get_content($url);
		$array = array('type' => '1',
            'estart' => '<ul id="leftnavigation" class="leftnavigation">',
            'eend' => '<\/div>',
        );
		$cates = $this->get_data_html->load($html_content, $array);
		$cates = $this->get_data_html->removetag('<ul class=\'clearfix\'>', '<\/ul>',$cates[0]);
		//$cates = $this->get_data_html->removetag('<ul>', '<\/ul>',$cates);
		$cates = strip_tags($cates,'<a>');
		$array = array('type' => '1',
            'estart' => 'href="',
            'eend' => '"',
        );
		$cates = $this->get_data_html->load($cates, $array);
		foreach($cates as $cate){
			$url = 'http://www.download.com.vn'.$cate;
			$html_content = $this->get_data_html->get_content($url);
			$tcount = $this->get_data_html->GetBetween($html_content, '<div class="summary-info">', '</div>');
			if($tcount =='')
				$tcount = 10;
			$tcount = str_replace(array('Có tất cả','phần mềm','.'),"",$tcount);
			$tcount = trim($tcount);
			echo str_replace(array('/','index.aspx'),"",$cate).": ".$tcount."<br>";
			
		}
		
		//echo $html_content;
	}
	
	function fixcode(){
		$this->load->model("mcategory");
		$this->db->order_by("CAT_PARENT asc");
		$cates = $this->mcategory->gets();
		foreach ($cates as $cate){
			$arr_update = array();
			$arr_update['CAT_NAME'] = $this->seo->DecimalToUni($cate->CAT_NAME);
			
			$arr_update['CAT_CODE'] = $this->seo->seolink($arr_update['CAT_NAME']);
			if($cate->CAT_PARENT >0){
			$catcode = $this->getRootParent($cate->CAT_PARENT);
				$arr_update['CAT_CODE'] = $catcode."-".$arr_update['CAT_CODE'];
			}
			$arr_update['CAT_CODE'] = strtolower($arr_update['CAT_CODE']);
			$this->mcategory->update($cate->CAT_ID,'CAT_ID',$arr_update);
			
		}
	}
	
	function getRootParent($parentID){
		$tcate = $this->mcategory->get($parentID,'CAT_ID');
		if($tcate && $tcate->CAT_PARENT ==0){
			return $tcate->CAT_CODE;
		}
		 return $this->getRootParent($tcate->CAT_PARENT);
	}
	
	function xxxx(){
	
	echo $this->seo->DecimalToUni("Đồ hoạ m&#225;y t&#237;nh");
	}
	
}
